// system include.
#include <pthread.h>
#include <thread>
#include <iostream>
#include <fstream>
#include <string.h>
// apply include.
#include "../include/driver/driver_usart.h"
#include <mutex>


uint8_t recv_bud2[512]; // 根据数据的长度定义一个数组
Usart_Buf usartBufdtu;
void dtu()
{
    int32_t recv = 0;
    
    std::mutex lock;
    lock.lock();
    /// binary data buffer
    std::vector<uint8_t> cache_buffer; // 存储数据
    
    SerialDriver serial("/dev/ttyUSB0", BPS115200); // 串口驱动对象，包含了对串口的初始化

    while (1)
    {
        // 串口数据读取
        std::vector<uint8_t> data_buffer;
        int32_t read_result = serial.ReadBytes(data_buffer, 0, 100);
        //printf("read_result:%d\n", read_result);
        if(read_result == RECV_DONE){
            cache_buffer.insert(cache_buffer.end(), data_buffer.begin(), data_buffer.end());
        }
        size_t sentenseStart = 0;
        if(cache_buffer.size()>0){
            uint8_t* pos = cache_buffer.data();
            // prints the vector
            // printf("ttyUSB0:\n");
            // for (int i = 0; i < cache_buffer.size(); i++)
            //     printf("%x ",*pos++);
            // printf("\n");

            if(cache_buffer[0] == 0x64){ // 寻找包头
                size_t rtcmLen = cache_buffer[1];
                if(cache_buffer.size() >= rtcmLen){
                    if(cache_buffer[rtcmLen-2]==0x0d && cache_buffer[rtcmLen-1]==0x0a){ // 寻找包尾
                        printf("%x %x \n", cache_buffer[rtcmLen-2], cache_buffer[rtcmLen-1]);
                        // first element
                        uint8_t* pos = cache_buffer.data();
                        // prints the vector
                        printf("ttyUSB0:\n");
                        for (int i = 0; i < cache_buffer.size(); i++)
                            printf("%x ",*pos++);
                        printf("\n");
                        // 删包
                        std::vector<uint8_t> remaining_buffer; // 处理储存数据后剩余的数据
                        cache_buffer = remaining_buffer;
                        //remaining_buffer.insert(remaining_buffer.end(), cache_buffer.begin()+rtcmLen);
                    }
                }
            }
        }
        
        //sentenseStart++;
        // 判断接收到的数据
        
        //recv = serial.ReadBuf(usartBufdtu, 20);
        // if(recv > 0) { // 通过阻塞方式接收串口数据中断
        //     printf("dtu recv len: %d\r\n",recv);
        //     for(int i=0; i<recv; i++) {
        //         printf("%x ", recv_bud2[i]);
        //     }printf("\r\n");
        //     memset(recv_bud2, 0x00, 512);
        // }
    }
}
uint8_t recv_bud[500]; // 根据数据的长度定义一个数组
Usart_Buf usartBufrtk;
void rtk()
{
    int32_t recv = 0;
    std::mutex lock;
    lock.lock();
    SerialDriver serial("/dev/ttyUSB1", BPS115200); // 串口驱动对象，包含了对串口的初始化
    while (1)
    {
        // 串口数据读取
        std::vector<uint8_t> data_buffer;
        int32_t read_result = serial.ReadBytes(data_buffer, 0, 100);
        if(read_result == RECV_DONE){
            // first element
            uint8_t* pos = data_buffer.data();

            // prints the vector
            printf("ttyUSB1:\n");
            for (int i = 0; i < data_buffer.size(); i++)
                printf("%x ",*pos++);
            printf("\n");
        }
        //recv = serial.ReadBuf(usartBufrtk, 20);
        // if(recv > 0) { // 通过阻塞方式接收串口数据中断
        //     printf("rtk recv len: %d\r\n",recv);
        //     for(int i=0; i<recv; i++) {
        //         printf("%x ", recv_bud[i]);
        //     }printf("\r\n");
        //     memset(recv_bud, 0x00, 512);
        // }
    }
}

int main(int argc, char *argv[])
{
    std::thread p_thread_dtu = std::thread(dtu); // 只能这么写，不这么写会报错
    std::thread p_thread_rtk = std::thread(rtk); // 只能这么写，不这么写会报错
    while (1)
    {
        usleep(1000*1000);
        //printf("123\n");
        /* code */
        // 数据接收
    }
}
//}


